/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package OverlayNetwork;

//~--- non-JDK imports --------------------------------------------------------
import jxtaUtil.IDCreator;
import jxtaUtil.PeerAdvertisement;

import net.jxta.document.AdvertisementFactory;
import net.jxta.peer.PeerID;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.pipe.PipeID;

//~--- JDK imports ------------------------------------------------------------

import java.net.InetAddress;
import java.net.UnknownHostException;
import net.jxta.platform.NetworkManager;

/**
 *
 * @author Zacharias
 */
public class jxtaPeerSetup {
        private static PeerID      peerID;
        private static PipeID      pipeID;
        private static PeerGroupID pgID;
        private static NetworkManager manager;


    public static void startUp() {
        createPeerAd();
        startNetManager();
        startDiscoveryServer();
        startDiscoveryClient();
    }

    public static void shutDown(){
        manager.stopNetwork();
    }

    static void createPeerAd(){

        pgID = IDCreator.createInfraPeerGroupID(GUI.StartupPanel.getGroupID());

        if (GUI.StartupPanel.checkID()) {
            peerID = IDCreator.createPeerID(pgID, GUI.StartupPanel.getPeerID());
        } else {
            peerID = IDCreator.createNewPeerID(pgID);
        }

        if (GUI.StartupPanel.checkPipe()) {
            pipeID = IDCreator.createPipeID(pgID, GUI.StartupPanel.getPipeID());
        } else {
            pipeID = IDCreator.createNewPipeID(pgID);
        }

        AdvertisementFactory.registerAdvertisementInstance(PeerAdvertisement.getAdvertisementType(),
                new PeerAdvertisement.Instantiator());

        PeerAdvertisement peerAdv = new PeerAdvertisement();

        peerAdv.setGroupID(pgID);
        peerAdv.setID(peerID);
        peerAdv.setOsName(System.getProperty("os.name"));
        peerAdv.setOsArch(System.getProperty("os.arch"));

         peerAdv.setName(System.getProperty("user.name"));

        try {
            peerAdv.setIP(InetAddress.getLocalHost().getHostAddress());
        } catch (UnknownHostException ignored) {
            ignored.printStackTrace();
        }
        System.out.println(peerAdv.toString());

    }


    private static void startNetManager(){
                // Checks if rendezvous box checked, true sets node to rendezvous node,
        // false sets node to edge node.
        try {
            if (GUI.StartupPanel.checkRendStatus()) {
                manager = new NetworkManager(NetworkManager.ConfigMode.RENDEZVOUS, "FileSharing");
                System.out.println("Rendezvous Node Status");
            } else {
                manager = new NetworkManager(NetworkManager.ConfigMode.EDGE, "FileSharing");
                System.out.println("Edge Node Status");
            }
            manager.setInfrastructureID(pgID);
            manager.setPeerID(peerID);
            // TODO: Manager needs to be threaded? Assuming that it needs to constantly sit and listen.
            manager.startNetwork();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void startDiscoveryClient() {
        //throw new UnsupportedOperationException("Not yet implemented");
    }

    private static void startDiscoveryServer() {
        //throw new UnsupportedOperationException("Not yet implemented");
    }
}
